package com.li.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.li.bean.Article;
import com.li.bean.User;
import com.li.common.Dbutil;
import com.li.dao.UserDao;

public class UserDaoImpl implements UserDao {
	private Connection conn = null;
	private PreparedStatement pst = null;
	private ResultSet rs = null;
	private Dbutil db = new Dbutil();

	@Override
	public List<User> getArticle(String usern) {
		List<User> list = new ArrayList<User>();
		String sql = "SELECT a.content,u.username,a.time "
				+ " FROM li_t_user u "
				+ " LEFT JOIN li_t_article a ON u.id=a.userid "
				+ " WHERE u.username=?";

		try {
			conn = db.getConn();
			pst = conn.prepareStatement(sql);
			pst.setString(1, usern);
			rs = pst.executeQuery();
			while (rs.next()) {
				User user = new User();
				Article a = new Article();
				a.setContent(rs.getString("content"));
				user.setUsern(rs.getString("username"));
				a.setTime(rs.getString("time"));
				user.setArticle(a);
				list.add(user);
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;

	}

	@Override
	public boolean userAdd(User user) {
		boolean flag = false;
		String sql = "insert into li_t_user(username,userpwd,email) value(?,?,?)";

		try {
			conn = db.getConn();
			pst = conn.prepareStatement(sql);
			pst.setString(1, user.getUsern());
			pst.setString(2, user.getUserp());
			pst.setString(3, user.getEmail());
			int i = pst.executeUpdate();
			if (i > 0) {
				flag = true;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

}
